<?php
namespace app\cash\controller;
use think\Controller;
use think\Db;
use app\cash\model\OrderDetails;
use app\cash\model\StoreGoodsLog;
class Jiaoban extends Controller{
    /**
     * @Author lignyun
     * @Desc    检查登录
     * 2018-04-09
     */
    public function _initialize(){
        if(!session('cash_user')) {
            $this->redirect('@cash/login/index');
        }
    }

    /**
     * @Author      Lingyun
     * @Description 交班
     * @DateTime    2018-06-12
     * @Description
     * @return      [type]     [description]
     */
    public function index(){
        $today = $this->get_today();
        $casher_id = session('cash_user')['id'];
        $casher = db('system_user')->where('id',$casher_id)->find();

        $zhifubao = $this->get_amount($casher,1);
        $weixin = $this->get_amount($casher,2);
        $yue = $this->get_amount($casher,3);
        $xianjin = $this->get_amount($casher,4);
        $bank = $this->get_amount($casher,5);
        $meituan = $this->get_amount($casher,6);
        $elema = $this->get_amount($casher,7);

        $zong = $zhifubao+$weixin+$yue+$xianjin+$bank+$meituan+$elema;

        $discount_amount = $this->get_discount_amount($casher);
        $store = $this->get_store($casher);

        $html = '';
        $html .= '<tr><td colspan="4" style="font-size: large;text-align: center;">'.$store["title"].'</td></tr><tr><td colspan="4" style="font-size: large;text-align: center">&nbsp;</td></tr><tr><td colspan="4" style="text-align: left;font-size: smaller">' . $store["address"] . '</td></tr><tr><td colspan="4" style="text-align: left;font-size: smaller">收银员：' . $casher["realname"] . '</td></tr>';
        $html .='<tr><td colspan="4" style="text-align: center">—————————————————</td></tr>';

        $html .= '<tr><td colspan="4" style="text-align: left;font-size: smaller">现金：' . $xianjin.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">支付宝：'.$zhifubao.'</td></tr>><tr><td colspan="4" style=";text-align: left;font-size: smaller;">微信：'.$weixin.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">会员卡：'.$yue.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">银行卡：'.$bank.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">美团：'.$meituan.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">饿了么：'.$elema.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">销售折扣：'.$discount_amount.'</td></tr>';


        $html .='<tr><td colspan="4" style="text-align: left">—————————————————</td></tr>';
        $html .='<tr><td colspan="4" style="text-align: left">总收款：'.$zong.'</td></tr>';
        $html .='<tr><td colspan="4" style="text-align: center"></td></tr>';

        $html .='<tr><td colspan="4" style="text-align: left">日期'.date('Y-m-d',time()).'</td></tr>';

        if(empty($html)){
            $this->error('找不到收款信息，请稍后再试','');
        }else{
            $this->success('成功','',$html);
        }


    }

    /**
     * @Author      Lingyun
     * @Description 获取门店信息
     * @DateTime    2018-06-12
     * @Description
     * @return      [type]     [description]
     */
    public function get_store($cash){
        $store = db('store')->where('id',$cash['store_id'])->find();

        return $store;
    }

    /**
     * @Author      Lingyun
     * @Description 获取今日的时间
     * @DateTime    2018-06-12
     * @Description
     * @return      [type]     [description]
     */
    private function get_today(){
        $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;

        return ['start'=>$beginToday,'end'=>$endToday];
    }

    /**
     * @Author      Lingyun
     * @Description 统计现金收款数额
     * @DateTime    2018-06-12
     * @Description
     * @return      [type]     [description]
     */
    private function get_amount($casher,$pay_type){
        $map['store_id'] = ['eq',$casher['store_id']];
        $map['order_status'] = ['eq',4];
        $map['pay_status'] = ['eq',1];
        $map['pay_type'] = ['eq',$pay_type];
        $map['cashier'] = ['eq',$casher['id']];

        $today = $this->get_today();
        $map['pay_time'] = array(array('gt',$today['start']),array('lt',$today['end']),'AND');

        $final_amount = db('order')->where($map)->sum('final_amount');
        $pay_type_two = db('order')->where($map)->sum('pay_type_two');

        $result = $final_amount-$pay_type_two;
        $result = empty($result)?0:$result;

        return $result;

    }

    /**
     * @Author      Lingyun
     * @Description 获取优惠金额
     * @DateTime    2018-06-12
     * @Description
     * @return      [type]     [description]
     */
    private function get_discount_amount($casher){
        $map['store_id'] = ['eq',$casher['store_id']];
        $map['order_status'] = ['eq',4];
        $map['pay_status'] = ['eq',1];
        $map['cashier'] = ['eq',$casher['id']];
        $today = $this->get_today();
        $map['pay_time'] = array(array('gt',$today['start']),array('lt',$today['end']),'AND');

        $discount_amount = db('order')->where($map)->sum('discount_amount');

        return $discount_amount;
    }

    /**
     * @Author  lingyun
     * @Desc    日结
     * 2018-06-12
     */
    public function rejie(){
        $casher = $this->get_casher();
        $store = $this->get_store($casher[0]);
        $html = '<tr><td colspan="4" style="font-size: large;text-align: center">'.$store["title"].'</td></tr><tr><td colspan="4" style="font-size: large;text-align: center">&nbsp;</td></tr><tr><td colspan="4" style="text-align: left;font-size: smaller;">' . $store["address"] . '</td></tr>';
        $html .='<tr><td colspan="4" style="text-align: left">========================</td></tr>';
        $total_amount = 0;
        foreach($casher as $k => $v){
            $zhifubao = $this->get_amount($v,1);
            $weixin = $this->get_amount($v,2);
            $yue = $this->get_amount($v,3);
            $xianjin = $this->get_amount($v,4);
            $bank = $this->get_amount($v,5);
            $meituan = $this->get_amount($v,6);
            $elema = $this->get_amount($v,7);

            $zong = $zhifubao+$weixin+$yue+$xianjin+$bank+$meituan+$elema;
            $total_amount = $total_amount+$zong;

            $discount_amount = $this->get_discount_amount($v);


            $html .= '<tr><td colspan="4" style="text-align: left;font-size: smaller">收银员：' . $v["realname"] . '</td></tr>';
            $html .='<tr><td colspan="4" style="text-align: center">—————————————————</td></tr>';

            $html .= '<tr><td colspan="4" style="text-align: left;font-size: smaller">现金：' . $xianjin.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">支付宝：'.$zhifubao.'</td></tr>><tr><td colspan="4" style=";text-align: left;font-size: smaller;">微信：'.$weixin.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">会员卡：'.$yue.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">银行卡：'.$bank.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">美团：'.$meituan.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">饿了么：'.$elema.'</td></tr><tr><td colspan="4" style=";text-align: left;font-size: smaller;">销售折扣：'.$discount_amount.'</td></tr>';


            $html .='<tr><td colspan="4" style="text-align: left">—————————————————</td></tr>';
            $html .='<tr><td colspan="4" style="text-align: left">总收款：'.$zong.'</td></tr>';
            $html .='<tr><td colspan="4" style="text-align: center"></td></tr>';

//            $html .='<tr><td colspan="4" style="text-align: left">日期'.date('Y-m-d',time()).'</td></tr>';
            $html .='<tr><td colspan="4" style="text-align: left">========================</td></tr>';
        }
        $html .='<tr><td colspan="4" style="text-align: left">总收款'.$total_amount.'</td></tr>';
        $html .='<tr><td colspan="4" style="text-align: left">日期'.date('Y-m-d',time()).'</td></tr>';

        if(empty($html)){
            $this->error('没有收款信息，请稍后再试','');
        }else{
            $this->success('成功','',$html);
        }
    }

    /**
     * @Author  lingyun
     * @Desc    获取收银员信息
     */
    private function get_casher(){
        $store_id = session('cash_user')['store_id'];
        $casher = db('system_user')->where(['status'=>1,'is_deleted'=>0,'store_id'=>$store_id])->select();

        return $casher;
    }



}